<?php
namespace app\wap\model;

use think\Model;

class Project extends Model
{
    /**
     * 查询会员订单记录
     * @param $memberId 会员id
     * @author hey_heyang(635870453@qq.com)
     */
    public function getProjecList($where){
        $where['pu.status'] = 5;
        $where['pu.safemobile'] = 'YES';
        $data = $this
            ->field('p.*,pu.company_name,FROM_UNIXTIME(p.create_time) as time')
            ->alias('p')
            ->join('tf_project_user pu', 'p.initiator=pu.id')
            ->where($where)
            ->order('p.top desc,p.create_time desc')
            ->select();
        $list = empty($data)?array():$data->toArray();
        return $list;
    }
    /**
     * 查询认投项目详情
     * @param $project_id 项目id
     * @author hey_heyang(635870453@qq.com)
     */
    public function getProjecDetail($where){
        $where['pu.status'] = 5;
        $where['pu.safemobile'] = 'YES';
        $data = $this
            ->field('p.*,pu.company_name,pu.logo,pu.company_intro')
            ->alias('p')
            ->join('tf_project_user pu', 'p.initiator=pu.id')
            ->where($where)
            ->find();
        $list = empty($data)?array():$data->toArray();
        return $list;

    }


    /**
     * 查询会员认投项目列表
     * @param $where
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getMemberProjectList($where){
        $where['or.type'] = 1;
        $where['or.status'] = ['gt',1];
//        $or_ids=[16864,18377,18663,20155,23157,21085,18102,5878,11352,11883,15671,18598,18639,18834,18828,19058,17488,17925,7545];
//        $where['or.id'] = ['not in',$or_ids];
        $where['or.delete_time'] = ['EXP', 'IS NULL'];
        $field = "or.uid,p.id,p.project_name,p.has_authorize,sum(or.money) as money,or.create_times as time,or.project_id,or.rights_state";
        $data = $this
            ->alias('p')
            ->join('tf_order or', 'p.id=or.project_id')
            ->field($field)
            ->where($where)
            ->group('p.id')
            ->order('time desc')
            ->select();
        $list = empty($data)?array():$data->toArray();
        if(count($list)){
            $order_model = new Order();
            foreach($list as $k=>&$v){
                $money=0;
                if($v['project_id']==16 && $v['money']>0){
                    $p1=$order_model->field('sum(money)*0.2 as p1_money')->where(['project_id'=>16,'status'=>['gt',1],'type'=>1,'uid'=>$v['uid'],'delete_time'=>['EXP', 'IS NULL']])->find()->toArray();
                    $money=$p1['p1_money'];
                }elseif($v['project_id']==17 && $v['money']>0){
                    $p1=$order_model->field('sum(money)*0.2 as p1_money')->where(['project_id'=>17,'status'=>['gt',1],'type'=>1,'uid'=>$v['uid'],'delete_time'=>['EXP', 'IS NULL']])->find()->toArray();
                    $money=$p1['p1_money'];
                }elseif($v['project_id']==30 && $v['money']>0){
                    $p2=$order_model->field('sum(money)*0.4 as p2_money')->where(['project_id'=>30,'status'=>['gt',1],'type'=>1,'uid'=>$v['uid'],'delete_time'=>['EXP', 'IS NULL']])->find()->toArray();
                    $money=$p2['p2_money'];
                }elseif($v['project_id']==35 && $v['money']>0){
                    $p2=$order_model->field('sum(money)*0.4 as p2_money')->where(['project_id'=>35,'status'=>['gt',1],'type'=>1,'uid'=>$v['uid'],'delete_time'=>['EXP', 'IS NULL']])->find()->toArray();
                    $money=$p2['p2_money'];
                }
                $v['money'] = $v['money']-$money;
            }
        }
        return $list;
    }


}